package com.windowes.weichang.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.IssueMapper;
import com.windowes.weichang.pojo.Issue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/6/2 下午8:54
 * @description:
 *
 **/
@Service
public class IssueService {


    @Autowired
    IssueMapper issueMapper;


    /**
     * @return
     */
    public ServerResponse addOrUpdate(Issue feedback) {
        if (feedback == null) {
            return ServerResponse.createByErrorMessage("操作失败!");
        }

        if (StringUtils.isEmpty(feedback.getContent())) {
            return ServerResponse.createByErrorMessage("请提交反馈信息!");
        }

        if (feedback.getId() == null) {
            issueMapper.insertSelective(feedback);
        } else {
            issueMapper.updateByPrimaryKeySelective(feedback);
        }

        return ServerResponse.createBySuccessMessage("操作成功!");
    }


    public ServerResponse delete(Integer id) {
        if (id == null) {
            return ServerResponse.createBySuccessMessage("请提交评论id!");
        }
        issueMapper.deleteByPrimaryKey(id);
        return ServerResponse.createBySuccessMessage("操作成功!");
    }


    public ServerResponse list(int pageNo, int pageSize) {
        Page<Issue> page = new Page<>(pageNo, pageSize);
        page.setRecords(issueMapper.selectByPage(page));
        return ServerResponse.createBySuccess(page);
    }

}